
/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.waveprotocol.wave.model.id {

/**
 * A wave is identified by a tuple of a wave provider domain and a local
 * identifier which is unique within the domain.
 */
public final class WaveId {
	private  var domain:String;
	private var id:String;
	
	/**
	 * Creates a WaveId from a serialized wave id.
	 * @param waveIdString a serialized wave id
	 * @return a WaveId
	 */
	public static function deserialise(waveIdString:String):WaveId {
		return LongIdSerialiser.INSTANCE.deserialiseWaveId(waveIdString);
	}
	
	/**
	 * @param domain must not be null. This is assumed to be of a valid canonical domain format.
	 * @param id must not be null. This is assumed to be escaped with
	 *   SimplePrefixEscaper.DEFAULT_ESCAPER.
	 */
	public function WaveId(domain:String, id:String) {
		if (domain == null || id == null) {
			throw new Error("Cannot create WaveId with null value in [domain:" + domain +
				"] [id:" + id + "]");
		}
		this.domain = domain;
		this.id = id;
	}
	public function getDomain():String {
		return domain;
	}
	public function getId():String {
		return id;
	}
	public function serialise():String {
		return LongIdSerialiser.INSTANCE.serialiseWaveId(this);
	}
	
	public function equals(obj:Object):Boolean {
		if (this == obj) return true;
		if (obj == null) return false;
		var other:WaveId= WaveId(obj);
		if (!domain == (other.domain)) return false;
		if (!id == (other.id)) return false;
		return true;
	}
	
	
	public function toString():String {
		return "[WaveId:" + serialise() + "]";
	}
	
	public function compareTo(a:Object):int {
		var other:WaveId=WaveId(a);
		var domainCompare:int= domain.localeCompare(other.domain);
		if (domainCompare == 0) {
			return id.localeCompare(other.id);
		} else {
			return domainCompare;
		}
	}
}
}